package com.lxt.basicmvc.net;

import android.text.TextUtils;

import com.blankj.utilcode.util.AppUtils;
import com.lxt.basicmvc.utils.SP;

import java.io.IOException;

import okhttp3.FormBody;
import okhttp3.HttpUrl;
import okhttp3.Interceptor;
import okhttp3.Request;
import okhttp3.RequestBody;
import okhttp3.Response;
import okio.Buffer;

/**
 * @author Joyson
 * @create 2019-04-10-14:17
 */

public class BaseParamsInterceptor implements Interceptor {
    private String TAG=BaseParamsInterceptor.class.getSimpleName();

    @Override
    public Response intercept(Chain chain) throws IOException {
        Request original = chain.request();
        //添加请求参数
        //                formBodyBuilder.add("userId", SpCacheUtils.getUserId());
//                formBodyBuilder.add("token", SpCacheUtils.getToken());
//                formBodyBuilder.add("appName", AppUtils.getAppName());
//                formBodyBuilder.add("appVersion", AppUtils.getAppVersionName());
//                formBodyBuilder.add("timestamp", String.valueOf(System.currentTimeMillis()));
//                formBodyBuilder.add("appOs", "android");
        //方式一：会将参数打印在log中
        HttpUrl url;
        //token没有说明没有的登录则不传 userId 和 token
//        if (TextUtils.isEmpty(SpCacheUtils.getToken())){
//            url=original.url().newBuilder()
//                    .addQueryParameter("appName", AppUtils.getAppName())
//                    .addQueryParameter("appVersion", AppUtils.getAppVersionName())
//                    .addQueryParameter("timestamp", TimeUtils.getNowTimeString())
//                    .addQueryParameter("appOs", "android")
//                    .build();
//        }else {
//             url=original.url().newBuilder()
//                    .addQueryParameter("userId", SpCacheUtils.getUserId())
//                    .addQueryParameter("token", SpCacheUtils.getToken())
//                    .addQueryParameter("appName", AppUtils.getAppName())
//                    .addQueryParameter("appVersion", AppUtils.getAppVersionName())
//                    .addQueryParameter("timestamp", TimeUtils.getNowTimeString())
//                    .addQueryParameter("appOs", "android")
//                    .build();
//        }
//        if (TextUtils.isEmpty(SpCacheUtils.getToken())){
//            url=original.url().newBuilder()
//                    .addQueryParameter("appName", AppUtils.getAppName())
//                    .addQueryParameter("appVersion", String.valueOf(AppUtils.getAppVersionCode()))
//                    .addQueryParameter("timestamp", TimeUtils.getNowTimeString())
//                    .addQueryParameter("appOs", "android")
//                    .build();
//        }else {
//            url=original.url().newBuilder()
//                    .addQueryParameter("userId", SpCacheUtils.getUserId())
//                    .addQueryParameter("token", SpCacheUtils.getToken())
//                    .addQueryParameter("appName", AppUtils.getAppName())
//                    .addQueryParameter("appVersion", String.valueOf(AppUtils.getAppVersionCode()))
//                    .addQueryParameter("timestamp", TimeUtils.getNowTimeString())
//                    .addQueryParameter("appOs", "android")
//
//                    .build();
//        }
//
//        //添加请求头
//        Request request = original.newBuilder()
//                .addHeader("Content-Type", "application/x-www-form-urlencoded; charset=UTF-8")
//                .addHeader("Connection", "keep-alive")
//                .method(original.method(), original.body())
//                .url(url)
//                .build();
//        return chain.proceed(request);
        //方式二：不会将参数打印在log中

        String userId = (String) SP.get(SP.userid,"");
        String token = (String) SP.get(SP.token,"");
        Request request = chain.request();
        RequestBody requestBody = request.body();
        FormBody.Builder builder = new FormBody.Builder();
//        builder.addEncoded("mimidaiCode", mMimidaiCode);
//        builder.addEncoded("appName", AppUtils.getAppName());
//        builder.addEncoded("appVersion", AppUtils.getAppVersionName());
        builder.addEncoded("appName", AppUtils.getAppName());
        builder.addEncoded("appVersion", AppUtils.getAppVersionName());
        builder.addEncoded("appOs", "android");
        //获取当前时间
//        builder.addEncoded("timestamp", TimeUtils.getNowTimeString());
        if (!TextUtils.isEmpty(token)) {
            builder.addEncoded("userId", userId);
            builder.addEncoded("token", token);
        }
        if (requestBody instanceof FormBody) {
            FormBody formBody = (FormBody) requestBody;
            for (int i = 0; i < formBody.size(); i++) {    // 如果要对已有的参数做进一步处理可以这样拿到参数
                builder.addEncoded(formBody.encodedName(i), formBody.encodedValue(i));
            }
            request = request.newBuilder().post(builder.build()).build();  //重新构建
        } else {
            request = request.newBuilder().method(request.method(), requestBody).post(builder.build()).build();  //重新构建
        }
        return chain.proceed(request);
    }

    private static String bodyToString(final RequestBody request) {
        try {
            final RequestBody copy = request;
            final Buffer buffer = new Buffer();
            if (copy != null)
                copy.writeTo(buffer);
            else
                return "";
            return buffer.readUtf8();
        } catch (final IOException e) {
            return "did not work";
        }
    }

}